<html>
<head>
  <meta charset="UTF-8">
  <title>Web Push Demo</title>
  <link rel="manifest" href="manifest.json">
</head>
<body>
  <h1>Test Page</h1>

<script type="text/javascript">
function base64UrlToUint8Array(base64UrlData) {
  const padding = '='.repeat((4 - base64UrlData.length % 4) % 4);
  const base64 = (base64UrlData + padding)
    .replace(/\-/g, '+')
    .replace(/_/g, '/');
  const rawData = atob(base64);
  const buffer = new Uint8Array(rawData.length);
  for (let i = 0; i < rawData.length; ++i) {
    buffer[i] = rawData.charCodeAt(i);
  }
  return buffer;
}

(function() {
  if (!('serviceWorker' in navigator)) {
    return;
  }

  return navigator.serviceWorker.register('service-worker.js')
  .then(function(registration) {
    console.log('service worker registered');
    return navigator.serviceWorker.ready;
  })
  .then(function(reg) {
    var channel = new MessageChannel();
    channel.port1.onmessage = function(e) {
      window.document.title = e.data;
    }
    reg.active.postMessage('setup', [channel.port2]);

    var subscribeOptions = { userVisibleOnly: true };
    // Figure out the vapid key
    var searchParam = window.location.search;
    vapidRegex = searchParam.match(/vapid=(.[^&]*)/);
    if (vapidRegex) {
      // Convert the base 64 encoded string
      subscribeOptions.applicationServerKey = base64UrlToUint8Array(vapidRegex[1]);
    }

    console.log(subscribeOptions);

    return reg.pushManager.subscribe(subscribeOptions);
  })
  .then(function(subscription) {
    console.log(JSON.stringify(subscription));
    window.subscribeSuccess = true;
    window.testSubscription = JSON.stringify(subscription);
  })
  .catch(function(err) {
    window.subscribeSuccess = false;
    window.subscribeError = err;
  });
})();
</script>
</body>
</html>
